//
//  VersionsViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/30.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "VersionsViewController.h"
#import "MessageStatus.h"
@interface VersionsViewController ()<UIAlertViewDelegate,ToyUpdateMessageStatusDelegate>

@end

@implementation VersionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    _LB_VersionFileSize.text = [NSString stringWithFormat:@"%@",_versionsDictionary[@"size"]];
    _LB_VersionFucn.text = [NSString stringWithFormat:@"%@",_versionsDictionary[@"note"]];
    
    _LB_VersionNumber.text = [NSString stringWithFormat:@"%@",_versionsDictionary[@"hw"]];
    
    _LB_VersionUpdateTime.text = @"123";
    
    
    
    
    if (self.isNew)
    {
        [_BT_update setTitle:NSLocalizedString(@"更新", nil) forState:UIControlStateNormal];
    }else
    {
        [_BT_update setEnabled:NO];
        [_BT_update setTitle:NSLocalizedString(@"已是最新版", nil) forState:UIControlStateNormal];
        [_BT_update setBackgroundColor:[UIColor grayColor]];
    }
    self.updateAlertView = [[UIAlertView alloc]initWithTitle:@"更新狀態" message:@"更新中請勿關閉娃娃與APP" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];

}
- (IBAction)updateEvent:(id)sender
{
    
    [[[UIAlertView alloc]initWithTitle:@"注意" message:@"請先確認娃娃是否開啟，並且更新中請勿關閉娃娃" delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:NSLocalizedString(@"確認", nil),nil]show];
}



-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        
        NSString *updateString = [NSString stringWithFormat:@"OTAUPDATE %@ %@ %@ %@ xmpp.mooredoll.com %@",self.toyInfo.jid_noDomain,_versionsDictionary[@"version"],_versionsDictionary[@"size"],_versionsDictionary[@"md5"],_versionsDictionary[@"url"]];
        
        xclient_message_send((char *)[self.toyInfo.jid UTF8String], (char *)[updateString UTF8String]);
        [self.updateAlertView show];

        [MessageStatus sharedInstance].toyUpdateMessageStatusDelegate = self;
    }
}

-(void)toyUpdateStatusStart:(NSString *)status
{
    [self.updateAlertView setTitle:status];
    
}
-(void)toyUpdateStatusStarting:(NSString *)status
{
    [self.updateAlertView setTitle:status];

}
-(void)toyUpdateStatusError:(NSString *)status
{
    [self.updateAlertView setTitle:status];

}
-(void)toyUpdateStatusTimeOur:(NSString *)status
{
    [self.updateAlertView setTitle:status];

}
-(void)toyUpdateStatusProgress:(NSString *)status total:(float)total received:(float)received
{
    [self.updateAlertView setTitle:status];

    
    [self.updateProgress setProgress:received/total animated:YES];
}


-(void)toyUpdateStatusMD5Start:(NSString *)status
{
    [self.updateAlertView setTitle:status];

}

-(void)toyUpdateStatusMD5Fail:(NSString *)status
{
    [self.updateAlertView setTitle:status];

}
-(void)toyUpdateStatusMD5Finish:(NSString *)status
{
    [self.updateAlertView setTitle:status];

}
-(void)toyUpdateFinishInfo:(NSDictionary *)InfoResults
{
    
    [self.updateAlertView dismissWithClickedButtonIndex:0 animated:YES];

    
    [_BT_update setEnabled:NO];
    [_BT_update setTitle:NSLocalizedString(@"已是最新版", nil) forState:UIControlStateNormal];
    [_BT_update setBackgroundColor:[UIColor grayColor]];

    [self.updateProgress setProgress:0];
    
}

-(void)viewDidAppear:(BOOL)animated
{
    _funcHeight.constant = _LB_VersionFucn.contentSize.height;
    [self.view layoutIfNeeded];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
